#include "matwin.h"
#include <QGraphicsView>
#include <QVBoxLayout>
#include "matscene.h"

MatWin::MatWin(const Mat &mat, QWidget *parent)
    : QWidget(parent),
      m_mat(std::move(mat)),
      m_matScene(Q_NULLPTR),
      m_graphView(Q_NULLPTR)
{
    initUi(mat);
}

MatWin::MatWin(qint32 m, qint32 n, qreal size, QWidget *parent)
    : MatWin(Mat(m, n, size), parent)
{
}

MatWin::~MatWin()
{
}

MatItem *MatWin::itemAt(qint32 m, qint32 n)
{
    return m_matScene->itemAt(m, n);
}

void MatWin::setItemsColor(qint32 type, const QColor &color)
{
    m_matScene->setItemsColor(type, color);
}

void MatWin::initUi(const Mat &mat)
{
    if (objectName().isEmpty())
        setObjectName(QStringLiteral("MatWin"));

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    qreal w = mat.width();
    qreal h = mat.height();
    //resize(w, h);
    QRectF sceneRect(0, 0, w, h);
    m_matScene = new MatScene(mat, sceneRect, this);

    m_graphView = new QGraphicsView(m_matScene, this);
    m_graphView->setStyleSheet("border: 0px");
    m_graphView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_graphView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    mainLayout->addWidget(m_graphView);
    mainLayout->setStretch(0, 1);
}
